package com.ruoyi.manage.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.manage.domain.EmpContract;
import com.ruoyi.manage.service.IEmpContractService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 员工合同Controller
 * 
 * @author cf
 * @date 2025-06-04
 */
@RestController
@RequestMapping("/manage/contract")
public class EmpContractController extends BaseController
{
    @Autowired
    private IEmpContractService empContractService;

    /**
     * 查询员工合同列表
     */
    @PreAuthorize("@ss.hasPermi('manage:contract:list')")
    @GetMapping("/list")
    public TableDataInfo list(EmpContract empContract)
    {
        startPage();
        List<EmpContract> list = empContractService.selectEmpContractList(empContract);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('manage:contract:list')")
    @GetMapping("/countInduction")
    public AjaxResult countInduction()
    {
        List<Map<String, Object>> monthCounts = empContractService.countStaffBySignDateMonth();

        // 中文月份映射
        String[] monthNames = {
                "一月", "二月", "三月", "四月",
                "五月", "六月", "七月", "八月",
                "九月", "十月", "十一月", "十二月"
        };

        // 构建结果
        List<Map<String, Object>> result = new ArrayList<>();

        for (Map<String, Object> map : monthCounts) {
            // 强制转换为 String
            String monthStr = (String) map.get("month"); // ✅ 正确方式

            // 强制转换为 Integer（支持 Long -> Integer）
            Integer count = ((Number) map.get("count")).intValue();

            String[] parts = monthStr.split("-");
            int monthNum = Integer.parseInt(parts[1]); // 提取月份数字

            if (monthNum >= 1 && monthNum <= 12) {
                Map<String, Object> item = new HashMap<>();
                item.put("month", monthNames[monthNum - 1]);
                item.put("value", count);
                result.add(item);
            }
        }

        return AjaxResult.success(result);
    }


    /**
     * 导出员工合同列表
     */
    @PreAuthorize("@ss.hasPermi('manage:contract:export')")
    @Log(title = "员工合同", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, EmpContract empContract)
    {
        List<EmpContract> list = empContractService.selectEmpContractList(empContract);
        ExcelUtil<EmpContract> util = new ExcelUtil<EmpContract>(EmpContract.class);
        util.exportExcel(response, list, "员工合同数据");
    }

    /**
     * 获取员工合同详细信息
     */
    @PreAuthorize("@ss.hasPermi('manage:contract:query')")
    @GetMapping(value = "/{empContractId}")
    public AjaxResult getInfo(@PathVariable("empContractId") Long empContractId)
    {
        return success(empContractService.selectEmpContractByEmpContractId(empContractId));
    }

    /**
     * 新增员工合同
     */
    @PreAuthorize("@ss.hasPermi('manage:contract:add')")
    @Log(title = "员工合同", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EmpContract empContract)
    {
        return toAjax(empContractService.insertEmpContract(empContract));
    }

    /**
     * 修改员工合同
     */
    @PreAuthorize("@ss.hasPermi('manage:contract:edit')")
    @Log(title = "员工合同", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EmpContract empContract)
    {
        return toAjax(empContractService.updateEmpContract(empContract));
    }

    /**
     * 删除员工合同
     */
    @PreAuthorize("@ss.hasPermi('manage:contract:remove')")
    @Log(title = "员工合同", businessType = BusinessType.DELETE)
	@DeleteMapping("/{empContractIds}")
    public AjaxResult remove(@PathVariable Long[] empContractIds)
    {
        return toAjax(empContractService.deleteEmpContractByEmpContractIds(empContractIds));
    }
}
